package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsCatalog;
import com.originskyseed.platform.cms.service.ICmsCatalogService;
import com.originskyseed.platform.cms.vo.request.CmsCatalogListRequestVO;
import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;

/**
 * 内容目录Controller类
 *
 * @author LiangMingHui
 */
@Controller
@RequestMapping("/cmsCatalog")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsCatalogController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsCatalogController.class);

	@DubboReference
	private ICmsCatalogService cmsCatalogService;

	/**
	 * 消息目录列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping("/cmsCatalogList.html")
//	public ModelAndView cmsCatalogList(HttpServletRequest request, HttpServletResponse response) {
//		return new ModelAndView("cms_catalog/cmsCatalog_list");
//	}

	/**
	 * 消息目录编辑页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsCatalogEdit.json")
	@RequiredParams(names = { "id" })
	public BaseResponseBody cmsCatalogEdit(HttpServletRequest request, HttpServletResponse response) {
		BaseResponseBody result = null;
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			CmsCatalog cmsCatalog = this.cmsCatalogService.selectByPrimaryKey(id);

			model.put("cmsCatalog", cmsCatalog);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), model);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 加载消息目录树
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsCatalogTree.json")
	public BaseResponseBody cmsCatalogTree(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = this.getParameterString("mallId", null);
			if (StringUtils.isBlank(mallId)) {
				mallId = null;
			}
			String suppId = this.getParameterString("suppId", null);
			String shopId = this.getParameterString("shopId", null);
			Map<String, Object> map = new HashMap<>();
			map.put("mallId", mallId);
			map.put("isSys", 0);
			if (StringUtils.isNotEmpty(suppId)) {
				map.put("suppId", suppId);
			}
			if (StringUtils.isNotEmpty(shopId)) {
				map.put("shopId", shopId);
			}
			JSONArray cmsCatalogTree = this.cmsCatalogService.selectCmsCatalogTreeList(map);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_TREE_SUCCESS.getMsg()), cmsCatalogTree);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_TREE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 分页查询消息目录列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsCatalogList.json")
	@RequiredParams(names = { "parentId", "pageNo", "pageSize" })
	public BaseResponseBody cmsCatalogList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsCatalogListRequestVO requestVO = this.getParameterBean(CmsCatalogListRequestVO.class);

			requestVO.setIsSys(0);
			requestVO.setMallId(mallId);
			// 分页查询
			BasePage<CmsCatalog> page = new BasePage<CmsCatalog>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsCatalogService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存消息目录信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsCatalogSave.json")
	@RequiredParams(names = { "cmsCataCode", "nameEn", "priority" })
	@SysLog(module = "cms模块", operationDesc = "新增或修改消息目录信息")
	public BaseResponseBody cmsCatalogSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsCatalog cmsCatalog = this.getParameterBean(CmsCatalog.class);
			cmsCatalog.setMallId(mallId);
			cmsCatalog.setIsSys(0);
			// 是否存在消息目录代码
			if (!cmsCatalog.getCmsCataCode().equals(cmsCatalog.getOldCmsCataCode()) && this.cmsCatalogService.existCode(cmsCatalog)) {
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_CODE_EXIST.getMsg()), null);
			} else {
				if (StringUtils.isNotBlank(cmsCatalog.getId())) {
					this.cmsCatalogService.updateByPrimaryKey(cmsCatalog);
				} else {
					cmsCatalog.setIsSys(0);
					this.cmsCatalogService.insertSelective(cmsCatalog);
				}
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_SAVE_SUCCESS.getMsg()), true);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除消息目录信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsCatalogDelete.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "删除消息目录信息")
	public BaseResponseBody cmsCatalogDelete(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsCatalogService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 修改消息目录显示状态（启用、禁用）
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateCmsCatalog.json")
	@RequiredParams(names = { "id", "isVisible" })
	@SysLog(module = "cms模块", operationDesc = "修改消息目录状态")
	public BaseResponseBody updateCarousel(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsCatalog cmsCatalog = this.getParameterBean(CmsCatalog.class);

			this.cmsCatalogService.updateByPrimaryKeySelective(cmsCatalog);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_STATUS_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSCATALOG_STATUS_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询三级目录
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectThirdLevelCatalog.json")
	@RequiredParams(names = { "id" })
	public Map<String, Object> selectThirdLevelCatalog(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			List<CmsCatalog> replies = this.cmsCatalogService.selectThirdlevelList(id);
			map.put("replies", replies);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return map;
	}
}
